package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FrameworkManager;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.myaccident.PhotoImageView;

/**
 * Created by Administrator on 2016/5/14.
 */
public class AccidentPicAdapter extends BaseAdapter {

    private Context mContext;

    private EntityBean[] lists;

    public AccidentPicAdapter(Context context) {
        this.mContext = context;
    }

    public void setLists(EntityBean[] lists) {
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return (null == lists) ? 0 : lists.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_accidentpic_item, null);
            holder.picIV = (ImageView) convertView.findViewById(R.id.apic_iv);
            holder.infoTV = (TextView) convertView.findViewById(R.id.apic_tv_info);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EntityBean bean = lists[position];
        String url = bean.getString("imageurl");
        if (!TextUtils.isEmpty(url)) {
            ImageLoader.getInstance().displayImage(url, holder.picIV, CommontUtils.getOptions());
        }
        String info = bean.getString("imagetypename");
        if (TextUtils.isEmpty(info)) {
            holder.infoTV.setText("受损照片");
        } else {
            holder.infoTV.setText(info);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> PicLists = new ArrayList<String>();
                for (int i = 0; i < lists.length; i++) {
                    PicLists.add(lists[i].getString("imageurl"));
                }
                PhotoImageView view = new PhotoImageView(mContext, PicLists);
                view.setCurItem(position);
                FrameworkManager.getInstance().showNewForm(mContext, view);
            }
        });
        return convertView;
    }

    private class ViewHolder {
        private ImageView picIV;
        private TextView infoTV;
    }
}
